package com.czy.owner.ui.seckill;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.adapter.SecKillFragmentAdapter;
import com.czy.owner.global.Constants;
import com.czy.owner.net.utils.JsonUtil;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.ui.BaseFragment;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.PhoneUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;

public class SecKillActivity extends BaseActivity {
    @BindView(R.id.tab_layout_seckill)
    TabLayout tabLayoutSeckill;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    private String[] timeArr = new String[5];
    private List<BaseFragment> fragmentList = new ArrayList<>();
    private SecKillFragmentAdapter mFragmentAdapter;
    private int currentId = 0;
    private int PLATFORM_SEC_TYPE=0;
    private int STORE_SEC_TYPE=1;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_sec_kill;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText(R.string.sec_kill);
        getServerTime();


    }

    private void getServerTime() {
        RequestParams params = new RequestParams(Constants.APP_API_BASE + Constants.PUB_SERVER_TIME);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                String exp = JsonUtil.getJsonValuesString(result, "exp");
                boolean flag = JsonUtil.getJsonValuesBoolean(result, "flag");
                MyLog.e("asus",result);
                if (flag){
                    long serverTime=JsonUtil.getJsonValuesLong(result, "serverTime");
                    initTabs(serverTime);
                }
                else {
                    PhoneUtils.ShowToastMessage(SecKillActivity.this,exp);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                PhoneUtils.ShowToastMessage(SecKillActivity.this,ex.getMessage());
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

    private void initTabs(long serverTime) {
        MyLog.e("asus",serverTime+"");
        initTimes(serverTime);
        currentId = 0;
        fragmentList.clear();
        for (int i = 0; i < timeArr.length; i++) {
            SeckillFragment seckillFragment = new SeckillFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("type",i);
            bundle.putLong("time", serverTime+24*3600*1000*i);
            MyLog.e("asus",serverTime+24*3600*1000*i+"");
            bundle.putInt("fromType",getIntent().getIntExtra("fromType",PLATFORM_SEC_TYPE));
            seckillFragment.setArguments(bundle);
            fragmentList.add(seckillFragment);
        }
        mFragmentAdapter = new SecKillFragmentAdapter(getSupportFragmentManager(), fragmentList);
        viewPager.setAdapter(mFragmentAdapter);
        viewPager.setOffscreenPageLimit(timeArr.length);
        tabLayoutSeckill.setupWithViewPager(viewPager);
        for (int i = 0; i < timeArr.length; i++) {
            TabLayout.Tab tab = tabLayoutSeckill.getTabAt(i);
            tab.setCustomView(R.layout.item_seckill_tab);
            int phoneWidth = PhoneUtils.getPhoneWidth(this);
            int avgWidth = phoneWidth / 5;
            View view = tab.getCustomView();
            if (i != timeArr.length - 1 && i != 0) {
                view.setLayoutParams(new LinearLayout.LayoutParams(avgWidth - 4, LinearLayout.LayoutParams.MATCH_PARENT));

            } else {
                view.setLayoutParams(new LinearLayout.LayoutParams(avgWidth, LinearLayout.LayoutParams.MATCH_PARENT));
            }
            TextView tabSeckillTitle = (TextView) view.findViewById(R.id.tab_seckill_title);
            TextView tabSeckillSub = (TextView) view.findViewById(R.id.tab_seckill_sub);
            tabSeckillTitle.setText(timeArr[i]);
            if (i == 0) {
                tabSeckillSub.setText(R.string.panic_buying);
                view.setBackgroundResource(R.color.white);
                tabSeckillTitle.setTextColor(getResources().getColor(R.color.appThemeColor));
                tabSeckillSub.setTextColor(getResources().getColor(R.color.shop_cart_yellow));
            } else {
                tabSeckillSub.setText(R.string.coming_soon);
                view.setBackgroundResource(R.color.appThemeBackgroundColor);
                tabSeckillTitle.setTextColor(getResources().getColor(R.color.black));
                tabSeckillSub.setTextColor(getResources().getColor(R.color.appThemeSub2TitleColor));
            }

        }
        viewPager.setCurrentItem(currentId);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currentId = position;
                MyLog.e("asus", tabLayoutSeckill.getTabCount() + "");
                for (int i = 0; i < tabLayoutSeckill.getTabCount(); i++) {
                    View view = tabLayoutSeckill.getTabAt(i).getCustomView();
                    if (view != null) {
                        TextView tabSeckillTitle = (TextView) view.findViewById(R.id.tab_seckill_title);
                        TextView tabSeckillSub = (TextView) view.findViewById(R.id.tab_seckill_sub);
                        if (i == currentId) {
                            if (currentId == 0) {
                                view.setBackgroundResource(R.color.white);
                                tabSeckillTitle.setTextColor(getResources().getColor(R.color.appThemeColor));
                                tabSeckillSub.setTextColor(getResources().getColor(R.color.shop_cart_yellow));
                            } else {
                                view.setBackgroundResource(R.color.white);
                                tabSeckillTitle.setTextColor(getResources().getColor(R.color.shop_cart_yellow));
                                tabSeckillSub.setTextColor(getResources().getColor(R.color.appThemeSub2TitleColor));
                            }

                        } else {
                            view.setBackgroundResource(R.color.appThemeBackgroundColor);
                            tabSeckillTitle.setTextColor(getResources().getColor(R.color.black));
                            tabSeckillSub.setTextColor(getResources().getColor(R.color.appThemeSub2TitleColor));
                        }
                    }

                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    private void initTimes(long serverTime) {
        for (int i = 0; i < timeArr.length; i++) {
            if (i == 0) {
                timeArr[0] = "今日秒杀";
            } else {
                SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日");
                MyLog.e("serverTime",serverTime+24*3600*1000*i+"");
                timeArr[i] = sdf.format(serverTime+24*3600*1000*i);
            }
        }
    }

    @Override
    protected void updateViews() {

    }

}
